package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.service.impl.SetMealServiceimpl;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("新增套餐")
    public Result<Setmeal> addSetMeal(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐的信息为：{}",setmealDTO);
        Setmeal setmeal = setMealService.add(setmealDTO);
        return Result.success(setmeal);
    }

    /**
     * 套餐得分页查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> PageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("开始进行分页查询,查询参数为：{}",setmealPageQueryDTO);
        PageResult pageResult = setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询套餐
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> selectById(@PathVariable Long id){
        log.info("根据id查询套餐，查询得id为：{}",id);
        SetmealVO setmealVO = setMealService.selectById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmeal",key = "#setmealDTO.categoryId")
    @ApiOperation("修改套餐")
    public Result<Setmeal> updatewithdish(@RequestBody SetmealDTO setmealDTO){
        log.info("开始修改套餐：{}",setmealDTO);
        Setmeal setmeal = setMealService.updateWithDish(setmealDTO);
        return Result.success(setmeal);
    }

    /**
     * 修改套餐状态
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("套餐状态修改")
    public Result StartOrStop(@PathVariable Integer status,Long id){
        log.info("开始修改套餐状态：{}",status,id);
        setMealService.StartOrStop(status,id);
        return Result.success();
    }

    @DeleteMapping
    @CacheEvict(cacheNames = "setmael",allEntries = true)
    @ApiOperation("批量删除菜品")
    public Result delete(@RequestParam List<Long> ids){
        log.info("开始批量删除套餐：{}",ids);
        setMealService.del(ids);
        return Result.success();
    }
}
